package com.fjec.common.util;

import lombok.AllArgsConstructor;
import lombok.Data;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.List;

public class ImageUtil {

    /**
     * 根据字节数组构建图像对象
     *
     * @param bytes
     * @return
     * @throws Exception
     */
    public static Image getImageFromBytes(byte[] bytes) throws Exception {
        return ImageIO.read(new ByteArrayInputStream(bytes));
    }

    /**
     * 在图片上写字
     *
     * @param srcImg
     * @param wordList
     * @param format   jpg png
     * @return
     * @throws Exception
     */
    public static byte[] writeWords(Image srcImg, List<WriteWord> wordList, String format) throws Exception {
        if (srcImg == null) {
            System.out.println("addWaterMark srcImg is null");
        }
        int srcImgWidth = srcImg.getWidth(null);//获取图片的宽
        int srcImgHeight = srcImg.getHeight(null);//获取图片的高
        // 加水印
        BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bufImg.createGraphics();
        // 设置图片背景色是透明的，否则生成的图片可能会出现黑边
        bufImg = g.getDeviceConfiguration().createCompatibleImage(srcImgWidth, srcImgHeight, Transparency.TRANSLUCENT);
        g.dispose();
        g = bufImg.createGraphics();
        g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
        // 写文字
        for (WriteWord word : wordList) {
            g.setColor(word.getColor()); //根据图片的背景设置水印颜色
            g.setFont(word.getFont());              //设置字体
            // 使文字显示更清晰，否则文字可能模糊
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
            //设置水印的坐标
            int x = word.getX();
            int y = word.getY();
            if (x == -1) {
                x = (srcImgWidth - getWatermarkLength(word.getContent(), g)) / 2;
            }
            if (y == -1) {
                y = (srcImgHeight - getWatermarkHeight(word.getContent(), g)) / 2;
            }
            g.drawString(word.getContent(), x, y);  //画出水印
        }
        g.dispose();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(bufImg, format, bos);
        byte[] bytes = bos.toByteArray();
        return bytes;
    }

    /**
     * 获取文字长度
     *
     * @param waterMarkContent
     * @param g
     * @return
     */
    public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }

    /**
     * 获取文字高度
     *
     * @param waterMarkContent
     * @param g
     * @return
     */
    public static int getWatermarkHeight(String waterMarkContent, Graphics2D g) {
        int p = g.getFontMetrics(g.getFont()).getHeight();
        return p;
    }

    @Data
    @AllArgsConstructor
    public static class WriteWord {

        private int x;
        private int y;
        private String content;
        private Font font;
        private Color color;

    }

}


